/*
    Dao Sqlite
    Copyright (C) 2010 Belousov Oleg <belousov.oleg@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <sqlite3.h>
#include <dao.h>

#include "db.h"

DAO_INIT_MODULE;

static void sqlite_new(DaoContext *ctx, DValue *p[], int N) {
    char    *name = (char*) DString_GetMBS(p[0]->v.s);
    sqlite3 *db;
    int     res;

    res = sqlite3_open(name, &db);

    if (res == SQLITE_OK) {
        DaoContext_PutCData(ctx, db, &dbTyper);
    }
};

static DaoFuncItem sqliteMeth[] = {
    { sqlite_new, "sqlite(file:string) => db" },
    { NULL, NULL }
};

DaoTypeBase sqliteTyper = {
    "sqlite",
    NULL,
    NULL,           /* no constant number */
    sqliteMeth,     /* one method bar() */
    { NULL },       /* no parent type */
    NULL,
    NULL            /* free by free() */
};

int DaoOnLoad(DaoVmSpace *vms, DaoNameSpace *ns) {
    DaoTypeBase *typers[] = {
        &sqliteTyper,
        &dbTyper,
        NULL
    };

    DaoNameSpace_WrapTypes(ns, typers);
    return 0;
};
